package com.yss.reportworld.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.yss.common.core.constant.Constants;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.domain.YssResponse;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.common.security.annotation.InnerAuth;
import com.yss.reportworld.service.IModularService;
import com.yss.reportworld.service.IReportService;
import com.yss.reportworld.service.ISqlScriptService;
import com.yss.reportworld.util.TableInfoUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.reportworld.domain.Customer;
import com.yss.reportworld.service.ICustomerService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;

/**
 * 客户信息Controller
 *
 * @author yss
 * @date 2023-03-24
 */
@RestController
@RequestMapping("/customer")
public class CustomerController extends BaseController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IModularService modularService;
    @Autowired
    private IReportService reportService;
    @Autowired
    private ISqlScriptService sqlScriptService;

    /**
     * 查询客户信息列表
     */
    @RequiresPermissions("reportworld:customer:list")
    @GetMapping("/list")
    public AjaxResult list(Customer customer) {
        List<Customer> list = customerService.selectCustomerList(customer);
        return success(list);
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("reportworld:report:list")
    @Log(title = "报表信息", businessType = BusinessType.OTHER)
    @PostMapping("/init")
    public AjaxResult init(@RequestBody Customer customer) {
        try {
            TableInfoUtil.handleTableVersion(customer.getHisVersion()); // 替换表名
            AjaxResult ajax = AjaxResult.success();
            ajax.put("hisVersions", sqlScriptService.getReductionVersion(false));
            ajax.put("modularTree", modularService.selectModularTreeList());
            ajax.put("reportTree", reportService.selectReportTreeList());
            return ajax;
        } catch (Exception ex) {
            throw new BusinessException("初始化加载数据出错，请稍后重试！" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 导出客户信息列表
     */
    @RequiresPermissions("reportworld:customer:export")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Customer customer) {
        List<Customer> list = customerService.selectCustomerList(customer);
        EasyExeclUtil.export(response, list, Customer.class);
    }

    /**
     * 获取客户信息详细信息
     */
    @RequiresPermissions("reportworld:customer:query")
    @PostMapping(value = "/getCustomer")
    public AjaxResult getInfo(@RequestBody Customer customer) {
        return success(customerService.selectCustomerById(customer));
    }

    /**
     * 新增客户信息
     */
    @RequiresPermissions("reportworld:customer:add")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Customer customer) {
        return toAjax(customerService.addCustomer(customer));
    }

    /**
     * 修改客户信息
     */
    @RequiresPermissions("reportworld:customer:edit")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Customer customer) {
        return toAjax(customerService.editCustomer(customer));
    }

    /**
     * 删除客户信息
     */
    @RequiresPermissions("reportworld:customer:remove")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody Customer customer) {
        return toAjax(customerService.delCustomer(customer));
    }

    /**
     * 导出客户信息
     */
    @RequiresPermissions("reportworld:customer:downLicense")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/handleDownLicense")
    public void handleDownLicense(HttpServletResponse response, Customer customer) throws IOException {
        String strCustomer = customerService.customerBase64(customer);
        IOUtils.write(strCustomer, response.getOutputStream(), Constants.UTF8);
    }

    /**
     * 定时任务调用查询客户许可到期日期
     *
     * @return
     */
    @InnerAuth
    @GetMapping("/queryLicenseDueTime")
    public YssResponse<String> queryLicenseDueTime() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(customerService.queryLicenseDueTime());
        return YssResponse.ok(buffer.toString());
    }
}
